package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.feign.BehaviorFeign;
import com.heima.article.feign.UserFeign;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.ArticleInfoService;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.article.pojos.*;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @作者 itcast
 * @创建日期 2020/10/7 10:38
 **/
@Service
public class ArticleInfoServiceImpl implements ArticleInfoService {
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        Map<String,Object> resultMap = new HashMap<>();

        if(dto == null || dto.getArticleId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, dto.getArticleId()));
        if(apArticleConfig == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(!apArticleConfig.getIsDelete() && !apArticleConfig.getIsDown()){
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getArticleId()));

            resultMap.put("content",apArticleContent);
        }
        resultMap.put("config",apArticleConfig);
        return ResponseResult.okResult(resultMap);
    }
    @Autowired
    BehaviorFeign behaviorFeign;
    @Autowired
    ApCollectionMapper apCollectionMapper;
    @Autowired
    UserFeign userFeign;
    @Autowired
    ApAuthorMapper apAuthorMapper;

    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        // 参数校验
        if(dto == null || dto.getArticleId() == null ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 查询行为实体数据
        ApUser user = AppThreadLocalUtils.getUser();
        ApBehaviorEntry behaviorEntry = behaviorFeign.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if(behaviorEntry == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // {"isfollow":true,"islike":true,"isunlike":false,"iscollection":true}
        boolean isunlike=false,islike=false,isfollow=false,iscollection=false;
        // 查询点赞信息
        ApLikesBehavior apLikesBehavior = behaviorFeign.findLikeByArticleIdAndEntryId(dto.getArticleId(),behaviorEntry.getId(),(short)0);
        if(apLikesBehavior!=null && apLikesBehavior.getOperation()==ApLikesBehavior.Operation.LIKE.getCode()){
            islike = true;
        }
        // 查询不喜欢信息
        ApUnlikesBehavior unlikesBehavior = behaviorFeign.findUnLikeByArticleIdAndEntryId(dto.getArticleId(), behaviorEntry.getId());
        if(unlikesBehavior!=null && unlikesBehavior.getType() == ApUnlikesBehavior.Type.UNLIKE.getCode()){
            isunlike = true;
        }
        // 查询收藏信息
        ApCollection apCollection = apCollectionMapper.selectOne(
                Wrappers.<ApCollection>lambdaQuery()
                        .eq(ApCollection::getArticleId, dto.getArticleId())
                        .eq(ApCollection::getEntryId, behaviorEntry.getId())
                        .eq(ApCollection::getType, ApCollection.Type.ARTICLE)
        );
        if(apCollection!=null){
            iscollection = true;
        }
        // 查询关注信息
        ApAuthor author = apAuthorMapper.selectById(dto.getAuthorId());
        if(author!=null){
            ApUserFollow apUserFollow = userFeign.findByUserIdAndFollowId(user.getId(), author.getUserId());
            if(apUserFollow!=null){
                isfollow = true;
            }
        }
        // 封装返回结果
        Map<String,Object> result = new HashMap<>();
        result.put("islike",islike);
        result.put("isunlike",isunlike);
        result.put("iscollection",iscollection);
        result.put("isfollow",isfollow);
        return ResponseResult.okResult(result);
    }
}
